package com.meida.module.bus.provider.service.srmservice;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusSupplier;
import com.meida.module.bus.client.entity.srmapi.basedata.*;
import com.meida.module.bus.provider.service.BusProjectService;
import com.meida.module.bus.provider.service.BusSupplierService;
import com.meida.module.bus.provider.service.SrmContractsmatdtlService;
import com.meida.module.bus.provider.service.SrmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * SRM系统数据同步
 *
 * @author both
 * @date 2020-10-14
 */
@Service
public class SynchroSrmToPmsService {
    // ----SRM系统-----
    @Autowired
    private SrmProjectinfoMysqlService proMysqlService;
    @Autowired
    private SrmWbsarchitectureService wbsarchitectureService;
    @Autowired
    private SrmContractsService srmContractsService;
    @Autowired
    private SrmContractsmatdtlService srmContractsmatdtlService;
    @Autowired
    private SrmDeliveryService srmDeliveryService;
    @Autowired
    private SrmDeliverydtlService srmDeliverydtlService;
    @Autowired
    private SrmDemandplanService srmDemandplanService;
    @Autowired
    private SrmDemandplandtlService srmDemandplandtlService;
    @Autowired
    private SrmPurchaseorderService srmPurchaseorderService;
    @Autowired
    private SrmPurchaseorderdetailService srmPurchaseorderdetailService;
    @Autowired
    private SrmPurchasingrequisitionService srmPurchasingrequisitionService;
    @Autowired
    private SrmPurchasingrequisitiondtlService srmPurchasingrequisitiondtlService;
    @Autowired
    private SrmReceivingnoteService srmReceivingnoteService;
    @Autowired
    private SrmVendorService srmVendorService;
    @Autowired
    private SrmMaterialService srmMaterialService;
    // ----PMS系统-----
    @Autowired
    private BusProjectService busProjectService;
    @Autowired
    private BusSupplierService busSupplierService;

    // ----共同-----
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SrmSqlserCommonService srmSqlserCommonService;
    @Autowired
    private SrmMysqlCommonService srmMysqlCommonService;

    // 项目信息
    public void sync_project(Map<String, Object> params) {
        List<SrmProjectinfo> proBeanMysql = srmSqlserCommonService.getSrmProjectIno(params);
        if (0 < proBeanMysql.size()) {
            proMysqlService.saveBatch(proBeanMysql);
            List<BusProject> busProjects = Lists.newArrayList();
            proBeanMysql.stream().forEach(srmProjectinfo -> {
                BusProject busProject = new BusProject();
                busProject.setProjectId(srmProjectinfo.getProjectId());
                busProject.setProjectCode(srmProjectinfo.getProjectCode());
                busProject.setProjectName(srmProjectinfo.getProjectName());
                busProject.setProjectAddress(srmProjectinfo.getProjectAddress());
                busProject.setProjectCgg(srmProjectinfo.getBusinessName()); // 项目采购岗
                busProject.setProjectSwg(srmProjectinfo.getBusinessName()); // 项目商务岗
                busProject.setProjectCwg(srmProjectinfo.getSafetyName()); // 项目账务
                busProject.setProjectXm(srmProjectinfo.getManagerName()); // 项目经理
                busProject.setSuppliesCg(srmProjectinfo.getGoodsPurGroupName()); // 物资采购
                busProject.setBusinessCg(srmProjectinfo.getBusinessPurGroupName()); // 商务岗采购
                busProject.setProjectLinkMan(srmProjectinfo.getProjectContacts()); // 项目联系人
                busProject.setLinkTel(srmProjectinfo.getPhone1()); // 项目人电话
                busProject.setLinkEmail(srmProjectinfo.getEmail1()); // 项目联系人
                busProject.setMaterialsLinkMan(srmProjectinfo.getMaterialClerk()); // 材料员
                busProject.setMaterialsTel(srmProjectinfo.getPhone2()); // 材料员电话
                busProject.setMaterialsEmail(srmProjectinfo.getEmail2()); // 材料员邮箱
                busProject.setDeliveryLinkMan(srmProjectinfo.getReceivingClerk()); // 收货员名称
                busProject.setDeliveryTel(srmProjectinfo.getPhone3()); // 收货员电话
                busProject.setDeliveryEmai(srmProjectinfo.getEmail3()); // 收货员邮箱
                busProjects.add(busProject);
            });
            if (0 < busProjects.size()) {
                busProjectService.saveBatch(busProjects);
            }
        }
    }

    // WBS
    public void sync_wbs(Map<String, Object> params) {
        List<SrmWbsarchitecture> wbsMysqlSrm = srmSqlserCommonService.getSrmWbsarchitecture(params);
        if (0 < wbsMysqlSrm.size()) {
            wbsarchitectureService.saveBatch(wbsMysqlSrm);
        }
    }

    // 物料信息
    public void sync_material(Map<String, Object> params) {
        List<SrmMaterial> materialMysqlSrm = srmSqlserCommonService.getSrmMaterial(params);
        if (0 < materialMysqlSrm.size()) {
            srmMaterialService.saveBatch(materialMysqlSrm);
        }
    }

    // 供应商信息
    public void sync_vendor(Map<String, Object> params) {
        List<SrmVendor> srmVendors = srmSqlserCommonService.getSrmVendor(params);
        if (0 < srmVendors.size()) {
            srmVendorService.saveBatch(srmVendors);
            List<BusSupplier> busSuppliers = Lists.newArrayList();
            srmVendors.stream().forEach(srmMysqlObj->{
                BusSupplier busSupplier = new BusSupplier();
                busSupplier.setSupplierId(srmMysqlObj.getVendorId());
                busSupplier.setSupplierCode(srmMysqlObj.getVendorCode());
                busSupplier.setSupplierName(srmMysqlObj.getVendorName());
                busSuppliers.add(busSupplier);
            });
            busSupplierService.saveBatch(busSuppliers);
        }
    }

    // 收货信息
    public void sync_receivingnote(Map<String, Object> params) {
        List<SrmReceivingnote> receivingnoteMysqlSrm = srmSqlserCommonService.getSrmReceivingnote(params);
        if (0 < receivingnoteMysqlSrm.size()) {
            srmReceivingnoteService.saveBatch(receivingnoteMysqlSrm);
        }
    }

    // 合同信息【主信息 + 明细行】
    public void sync_contracts(Map<String, Object> params) {
        List<SrmContracts> contractsMysqlSrm = srmSqlserCommonService.getSrmContracts(params);
        if (0 < contractsMysqlSrm.size()) {
            srmContractsService.saveBatch(contractsMysqlSrm);
            contractsMysqlSrm.stream().forEach(
                contracts -> {
                    Map<String, Object> detailMap = Maps.newHashMap();
                    detailMap.put("contractsId", contracts.getContractsId());
                    List<SrmContractsmatdtl> srmContractsmatdtls = srmSqlserCommonService.getSrmContractsmatdtl(detailMap);
                    if (0 < srmContractsmatdtls.size()) {
                        srmContractsmatdtlService.saveBatch(srmContractsmatdtls);
                    }
                });
        }
    }

    // 收货信息【主信息 + 明细行】
    public void sync_delivery(Map<String, Object> params) {
        List<SrmDelivery> deliveryMysqlSrm = srmSqlserCommonService.getSrmDelivery(params);
        if (0 < deliveryMysqlSrm.size()) {
            srmDeliveryService.saveBatch(deliveryMysqlSrm);
            deliveryMysqlSrm.stream().forEach(
                delivery -> {
                    Map<String, Object> detailMap = Maps.newHashMap();
                    detailMap.put("deliveryId", delivery.getDeliveryId());
                    List<SrmDeliverydtl> srmDeliverydtls = srmSqlserCommonService.getSrmDeliverydtl(detailMap);
                    if (0 < srmDeliverydtls.size()) {
                        srmDeliverydtlService.saveBatch(srmDeliverydtls);
                    }
                });
        }
    }

    // 需求计划信息【主信息 + 明细行】
    public void sync_demandPlan(Map<String, Object> params) {
        List<SrmDemandplan> demandplanMysqlSrm = srmSqlserCommonService.getSrmDemandplan(params);
        if (0 < demandplanMysqlSrm.size()) {
            srmDemandplanService.saveBatch(demandplanMysqlSrm);
            demandplanMysqlSrm.stream().forEach(
                demandplan -> {
                    Map<String, Object> detailMap = Maps.newHashMap();
                    detailMap.put("planId", demandplan.getPlanid());
                    List<SrmDemandplandtl> srmDemandplandtls = srmSqlserCommonService.getSrmDemandplandtl(detailMap);
                    if (0 < srmDemandplandtls.size()) {
                        srmDemandplandtlService.saveBatch(srmDemandplandtls);
                    }
                });
        }
    }

    // 采购申请信息【主信息 + 明细行】
    public void sync_purchasingrequisition(Map<String, Object> params) {
        List<SrmPurchasingrequisition> purchasingrequisitionMysqlSrm = srmSqlserCommonService.getSrmPurchasingrequisition(params);
        if (0 < purchasingrequisitionMysqlSrm.size()) {
            srmPurchasingrequisitionService.saveBatch(purchasingrequisitionMysqlSrm);
            purchasingrequisitionMysqlSrm.stream().forEach(
                purchasingrequisition -> {
                    Map<String, Object> detailMap = Maps.newHashMap();
                    detailMap.put("purchasingRequisitionId", purchasingrequisition.getPurchasingRequisitionId());
                    List<SrmPurchasingrequisitiondtl> srmPurchasingrequisitiondtls = srmSqlserCommonService.getSrmPurchasingrequisitiondtl(detailMap);
                    if (0 < srmPurchasingrequisitiondtls.size()) {
                        srmPurchasingrequisitiondtlService.saveBatch(srmPurchasingrequisitiondtls);
                    }
                });
        }
    }

    // 采购订单信息【主信息 + 明细行】
    public void sync_purchaseorder(Map<String, Object> params) {
        List<SrmPurchaseorder> purchaseorderMysqlSrm = srmSqlserCommonService.getSrmPurchaseorder(params);
        if (0 < purchaseorderMysqlSrm.size()) {
            srmPurchaseorderService.saveBatch(purchaseorderMysqlSrm);
            purchaseorderMysqlSrm.stream().forEach(
                purchaseorder -> {
                    Map<String, Object> detailMap = Maps.newHashMap();
                    detailMap.put("purchaseorderId", purchaseorder.getPurchaseorderId());
                    List<SrmPurchaseorderdetail> srmPurchaseorderdetails = srmSqlserCommonService.getSrmPurchaseorderdetail(detailMap);
                    if (0 < srmPurchaseorderdetails.size()) {
                        srmPurchaseorderdetailService.saveBatch(srmPurchaseorderdetails);
                    }
                });
        }
    }
}
